//
//  CLLRCShowView.m
//  LRCAnalyze
//
//  Created by 周文成 on 2018/1/5.
//  Copyright © 2018年 zwc. All rights reserved.
//

#import "CLLRCShowView.h"
#import "CLLRCCell.h"
#import "CLLRC.h"

@interface CLLRCShowView () <UITableViewDelegate, UITableViewDataSource> {
    NSInteger selectedIndex;
}

@property (nonatomic, strong) CLLRC *lrc;

@end


@implementation CLLRCShowView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self configueProperty];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configueProperty];
    }
    return self;
}


- (void)configueProperty {
    self.delegate   = self;
    self.dataSource = self;
    self.rowHeight = 30;
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.bounces = NO;
    selectedIndex = -1;
}

- (void)setFilerUrl:(NSURL *)filerUrl {
    _filerUrl = filerUrl;
    self.lrc = [[CLLRC alloc] initWithFileUrl:filerUrl];
    [self reloadData];
}

- (void)setCurrentTime:(NSInteger)currentTime {
    
    _currentTime = currentTime;
    NSInteger index = [self.lrc indexWithTime:currentTime];
    if(selectedIndex != index && index != -1 && self.lrc.words.count > index){
        [self selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        selectedIndex = index;
    } 
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.lrc.words.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *idenifier = @"CLLRCCell"; 
    CLLRCCell *cell = [tableView dequeueReusableCellWithIdentifier:idenifier];
    if(!cell){
        cell = [[CLLRCCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idenifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CLLRCWord *word = [self.lrc.words objectAtIndex:indexPath.row];
    cell.textLabel.text = word.context;
    return cell;
}






@end






